--[[
Name: Prat BottleCap
Revision: $Revision: 78621 $
Author: Ethan Centaurai
Website: http://files.wowace.com/Prat/
Issues and feature requests: http://code.google.com/p/ethancentaurai/issues/list
Description: Converts ALL CAPS CHAT into all lower case chat
Dependencies: Prat
]]--


local PRAT_MODULE = Prat:RequestModuleName("BottleCap")
local BottleCap = Prat:NewModule(PRAT_MODULE)

BottleCap.revision	= ("$Revision: 78621 $"):match("%d+")
BottleCap.defaultDB	= { on = true, smart = true, }
BottleCap.moduleName	= "BottleCap"
BottleCap.moduleDesc	= "Converts ALL CAPS CHAT into all lower case chat"
BottleCap.moduleOptions	= {}
BottleCap.toggleOptions	= {}
BottleCap.consoleName	= "bottlecap"
BottleCap.guiName	= "Prat BottleCap"


function BottleCap:GetModuleOptions()
	self.moduleOptions = {
		name	= self.moduleName,
		desc	= self.moduleDesc,
		type	= "group",
		args = {
			{
				name = "Smart Filtering",
				desc = "If enabled, BottleCap will only convert ALL CAPS TEXT into lower case chat. If disabled, BottleCap will convert all text into lower case, regardless of original case (text with item links are unaffected).",
				type = "toggle", order = 1,
				get = function() return self.db.profile.smart end,
				set = function(v) self.db.profile.smart = v end,
			},
		}
	}

	return self.moduleOptions
end


function BottleCap:OnModuleEnable()
	self:RegisterEvent("Prat_PreAddMessage")
end

function BottleCap:OnModuleDisable()
	self:UnregisterAllEvents()
end

function BottleCap:Prat_PreAddMessage(message, frame, event, t, r, g, b)
	local msg = message.MESSAGE

	if msg:find("|[Hh]") then return end -- ignore chat with hyperlinks, we fuck things up otherwise

	if self.db.profile.smart then
		if msg == msg:upper() then message.MESSAGE = msg:lower() end -- ALL CAPS CHAT will become lowercase
	else
		message.MESSAGE = msg:lower() -- all chat will become lowercase
	end
end
